Network Programming in Java: Internet protocols (IP, UDP, TCP) by Alan MOUHLI

Network Programming in Java: Internet protocols (IP, UDP, TCP) by Alan MOUHLI

Author:Alan MOUHLI
Language: eng
Format: mobi
Publisher: Alan MOUHLI
Published: 2016-04-05T21:00:00+00:00


import java.util.*; import java.io.*; import java.net.*;

public class TimeSender implements AutoCloseable {

private final DatagramSocket socket; private final DatagramPacket packet;

public TimeSender(int port) throws SocketException {

this.socket = new DatagramSocket();

this.packet = new DatagramPacket(new byte[0], 0); // Packet with empty array

this.packet.setPort(port);

}

public void sendTimePacket(InetAddress a) throws IOException

{

// We reuse the same datagram packet but modify its content and addressee

byte[] timeData = new

Date().toString().getBytes();

packet.setData(timeData);

packet.setAddress(a);

// this.packet.setPort is useless since the port does not change

socket.send(packet); // May throw an IOException }

public static InetAddress getSuccessor(InetAddress a) {

// Exercise: write the implementation of getSuccessor(InetAddress) returning the successor of an IP address.

// Example: the successor of 10.1.2.3 is 10.1.2.4 (for last byte 0 and 255 are forbidden)

}

public void close() throws IOException

{

socket.close();

}

public static void main(String[] args) throws IOException {

if (args.length < 3) throw new

IllegalArgumentException("Not enough arguments"); InetAddress firstA =

InetAddress.getByName(args[0]); // Start address InetAddress lastA =

InetAddress.getByName(args[1]); // Stop address int port = Integer.parseInt(args[2]); try (TimeSender ts = new TimeSender(port)) {

for (InetAddress currentA = firstA; !

currentA.equals(lastA); currentA = getSuccessor(currentA)) ts.sendTimePacket(currentA); }

}

}

Some tips for the exchange of packets

 Recycle DatagramPacket already created (by changing the hosted data)

 Do not create unnecessary DatagramSocket, release them when no longer needed

 Always check that the receipt size is sufficient

classic mistake: send k byte packet and receive on the same package without changing table or size; it is then limited to k bytes for reception

 Do not forget to use the size of the data received: the end of the table is not exploitable

 Check the IP address / transmitter port of a received packet (even if it makes only a small safety)

Mechanism of pseudo-connection

 Possibility of pseudo-connect two UDP sockets with void

DatagramSocket.connect (InetAddress addr, int port): automatic filtering of packets sent and received

 Checking the connection withisConnected boolean ()

 Disconnection with void disconnect ()

Some tests with Netcat

Netcat: simple application to communicate with UDP (and TCP)

 We run the server on port UDP N listening on all interfaces (wildcard address): nc -l -u N

 It runs a client connecting to the port N of the machine:

n -u N MachineName

 The client sends each line of standard input in a UDP datagram: the server receives and displays it on its standard output

 server datagram sending to the customer also possible

Java example: receive datagrams on a port with a deadline



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.